//
//  UserConfigs.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/8/12.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//




import UIKit
import YSKit
import RxSwift

class UserConfiges: YSBaseOptionConfig {
    
    
    
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .next
        self.i18n = "VC.usercenter.title"
        
        // 通用设置
        self.setupNextConfigs([[HeadlogoConfig(), NameConfig(), AgeConfig(), SexConfig(), PhoneConfig()],
                               [HelpRecordConfig(), AppealConfig()],
                               [ShareCountConfig(), ShareKmConfig(), ShareTimeConfig()],
                               [MyPointConfig()]])
        
    }
    
}

class HeadlogoConfig: YSBaseOptionConfig {
    let iamgeView = UIImageView()
    public  init() {
        super.init(value: ("usercenter.headlogo.title", .next, nil))
        self.selectedMode = .next
        self.rowHeight = 80
        
    }
}



class NameConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .next
        self.i18n = "usercenter.name.title"
        
        let editConfig = YSBaseOptionConfig()
        editConfig.selectedMode = .text
        editConfig.text = UserService.sharedInstance.name.value
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.name.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (name) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = name ?? ""
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            HUD.show()
            _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.updateUserInfo(token: Auth.token, value: config.text!, type: .nickName))).subscribe(onNext: {(data) in
                //
                HUD.hide()
                
                UserService.sharedInstance.name.value = config.text

                }, onError: {(_) in
                    HUD.hide()
//                    self.handle(withError: ErrorType)
            })

        })
        

        
        self.setupNextConfigs([[editConfig]])
        
    }
}



class PhoneConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .none
        self.i18n = "usercenter.phone.title"
    
        let editConfig = YSBaseOptionConfig()
        editConfig.selectedMode = .text
        editConfig.text = UserService.sharedInstance.phone.value
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.phone.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (phone) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = phone ?? ""
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            UserService.sharedInstance.phone.value = config.text
        })
        
        self.setupNextConfigs([[editConfig]])

    }
    
    
}


class AgeConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .next
        self.i18n = "usercenter.age.title"
        
        let editConfig = YSBaseOptionConfig()
        editConfig.selectedMode = .text
        editConfig.text = UserService.sharedInstance.age.value
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.age.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (age) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = age ?? ""
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            HUD.show()
            _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.updateUserInfo(token: Auth.token, value: config.text!, type: .age))).subscribe(onNext: {(data) in
                //
                HUD.hide()
                
                UserService.sharedInstance.age.value = config.text
                
            }, onError: {(_) in
                HUD.hide()
                //                   
                //self.handle(withError: ErrorType)
            })
        })
        
        self.setupNextConfigs([[editConfig]])
        
    }
}

class HelpRecordConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .next
        self.i18n = "usercenter.helprecord.title"
        self.nextController = CommentVC.self
        
    }
}

class AppealConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .next
        self.i18n = "usercenter.appeal.title"
        
        
    }
}

class ShareCountConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .none
        self.i18n = "usercenter.sharecount.title"
        
        let editConfig = YSBaseOptionConfig()
        editConfig.text = UserService.sharedInstance.count.value
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.count.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (count) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = count ?? ""
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            UserService.sharedInstance.count.value = config.text
        })
        
        self.setupNextConfigs([[editConfig]])
        
    }
}

class ShareKmConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .none
        self.i18n = "usercenter.shareKm.title"
        
        let editConfig = YSBaseOptionConfig()
        editConfig.text = UserService.sharedInstance.km.value! + " km"
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.count.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (km) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = km! + " km"
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            UserService.sharedInstance.km.value = config.text
        })
        
        self.setupNextConfigs([[editConfig]])
    }
}

class ShareTimeConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .none
        self.i18n = "usercenter.sharetime.title"
        
        let editConfig = YSBaseOptionConfig()
        editConfig.text = UserService.sharedInstance.times.value! + " min"
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.count.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (time) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = time! + " min"
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            UserService.sharedInstance.times.value = config.text
        })
        
        self.setupNextConfigs([[editConfig]])

    }
}

class MyPointConfig: YSBaseOptionConfig {
    public  init() {
        super.init(value: ("", .next, nil))
        self.selectedMode = .next
        self.i18n = "usercenter.mypoint.title"

        let editConfig = YSBaseOptionConfig()
        editConfig.text = UserService.sharedInstance.point.value
        //UserService的name变化时，触发界面的名称显示的修改
        _  = UserService.sharedInstance.count.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (point) in
            guard let `self` = self else {
                return
            }
            self.subTitle.value = point ?? ""
        })
        
        //界面内容修改时，修改UserService
        _ = editConfig.selectedEvent.takeUntil(self.rx.deallocated).subscribe(onNext: { (config) in
            // self.value = config
            UserService.sharedInstance.point.value = config.text
        })
        
        self.setupNextConfigs([[editConfig]])
    }
}


class SexConfig: YSAppConfigSingle<Sex> {
    
    
    public override func saveValue(){
        
        //值为改变，这不保存
        guard self.value != UserService.sharedInstance.sex.value else{
            return
        }
        
        HUD.show()
        
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.updateUserInfo(token: Auth.token, value: "\(self.value.rawValue)", type: .sex))).subscribe(onNext: {(data) in
            //
            HUD.hide()
            
            UserService.sharedInstance.sex.value = self.value
            
        }, onError: {(error) in
            HUD.hide()
            //
            self.handle(withError: error)
        })

    }
    
    public override func loadValue() {
        UserService.sharedInstance.sex.value = self.value
    }
}




//
enum Sex: Int, YSEnumable {
    case unknow = 2
    case male = 0 //男性
    case female = 1 //女性
    
    var i18n: String {
        switch self {
        case .male:
            return I18n("usercenter.sex.male.title")
        case .female:
            return I18n("usercenter.sex.female.title")
        default:
            return I18n("usercenter.sex.unknow.title")
            
        }
    }
    var icon: UIImage? {
        return nil
    }
    static var defaultValue: Sex {
        return  .unknow
    }
    static var allValues: [Sex] {
        return  [.unknow, .male, .female]
    }
    static var title: String {
        return I18n("usercenter.sex.title")
    }
    
    public var value: AnyObject {
        switch self {
        case .male:
            return 0 as AnyObject
        case .female:
            return 1 as AnyObject
        default:
            return 2 as AnyObject
        }
    }
    public  static var key: String {
        return "sex"
    }
}
